package game.web.struts;

import game.web.base.Constants;
import game.web.base.User;
import game.web.controller.ChargeInitControllor;
import game.web.dao.dto.MyBankDTO;
import game.web.dao.dto.UserDTO;
import game.web.viewhelper.ChargeInitViewHelper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import projectx.util.ClassUtil;
import projectx.web.base.ConstantsProjectX;
import projectx.web.base.ProjectXSecurityAction;
import projectx.web.base.ProjectXForm;
import projectx.web.base.UserContext;
import projectx.web.base.navi.Function;

public class ChargeInitAction extends ProjectXSecurityAction {

	@Override
	protected ActionForward execute(User user, Function function,
			ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpSession session, HttpServletResponse response) throws Exception {
		debugBegin();

		UserContext uc = getUc();
		
		System.err.println("uc.userid="+uc.get(ConstantsProjectX.USER_ID));

		ChargeInitControllor.execute(getUc(), (ProjectXForm) form);
		// output
		debugEnd();
		
		if(!initViewHelper(request))
			return findForward(mapping, forwardFailure);
		
		return findForward(mapping, forwardSuccess);
	}

	@Override
	protected boolean isPostMethodOnly() {
		return false;
	}
	
	private boolean initViewHelper(HttpServletRequest request) {
		ChargeInitViewHelper viewHelper = new ChargeInitViewHelper();
		UserDTO userDTO = (UserDTO) getUcAttribute(Constants.CHARGE_INIT_USER_DTO);
		MyBankDTO mybankDTO = (MyBankDTO) getUcAttribute(Constants.CHARGE_INIT_MYBANK_TO_DTO);
		
		System.err.println("WithdrawInitAction UserDTO="+userDTO);
		if(userDTO != null)
			ClassUtil.copyData(userDTO, viewHelper);
		else
			return false;
		viewHelper.setBankinfo(mybankDTO.getBankinfo());
		
		request.setAttribute(ConstantsProjectX.VIEW_HELPER, viewHelper);
		return true;
	}
}
